<template>
  <div>
    <h2>反向父子配对树形结构</h2>
   
  </div>
</template>

<script>


export default {
  components: {
    
  },
  data() {
    return {
      rawData: [
    {
        "id": 145253,
        "user_id": {
            "name": "系统管理员",
            "type": ""
        },
        "update_user_id": {
            "name": "",
            "type": ""
        },
        "frame_id": "",
        "owner_user_id": {
            "name": "",
            "type": ""
        },
        "created_at": null,
        "updated_at": null,
        "peijianxuhao": 1,
        "daogongxu": "外发深镭射",
        "shengchanzhouqi": 13,
        "gongyinandu": null,
        "laiyuan": "自制",
        "peijianleixing": "白架",
        "bengongxuid": "烧焊",
        "bengongxu": "烧焊",
        "daogongxuid": "外发深镭射",
        "daopeijianleixing": "",
        "wuliaodangan": "GME0679-55-35J",
        "baogong": 1,
        "liuchengtu": null,
        "gongjia": null,
        "beizhu": null,
        "create_user": {
            "id": 1,
            "name": "系统管理员",
            "avatar": "[]",
            "info": null
        },
        "update_user": null,
        "owner_frame": null,
        "owner_user": null,
        "is_share": false,
        "wuliaodanganid": 78821
    },
    {
        "id": 146193,
        "user_id": {
            "name": "系统管理员",
            "type": ""
        },
        "update_user_id": {
            "name": "",
            "type": ""
        },
        "frame_id": "",
        "owner_user_id": {
            "name": "",
            "type": ""
        },
        "created_at": null,
        "updated_at": null,
        "peijianxuhao": 2,
        "daogongxu": "金打磨",
        "shengchanzhouqi": 2,
        "gongyinandu": null,
        "laiyuan": "外发",
        "peijianleixing": "白架",
        "bengongxuid": "外发深镭射",
        "bengongxu": "外发深镭射",
        "daogongxuid": "金打磨",
        "daopeijianleixing": "",
        "wuliaodangan": "GME0679-55-35J",
        "baogong": 1,
        "liuchengtu": null,
        "gongjia": null,
        "beizhu": null,
        "create_user": {
            "id": 1,
            "name": "系统管理员",
            "avatar": "[]",
            "info": null
        },
        "update_user": null,
        "owner_frame": null,
        "owner_user": null,
        "is_share": false,
        "wuliaodanganid": 78821
    },
    {
        "id": 11519,
        "user_id": {
            "name": "系统管理员",
            "type": ""
        },
        "update_user_id": {
            "name": "",
            "type": ""
        },
        "frame_id": "",
        "owner_user_id": {
            "name": "",
            "type": ""
        },
        "created_at": null,
        "updated_at": null,
        "peijianxuhao": 3,
        "daogongxu": "外发电镀",
        "shengchanzhouqi": 6,
        "gongyinandu": null,
        "laiyuan": "自制",
        "peijianleixing": "白架",
        "bengongxuid": "金打磨",
        "bengongxu": "金打磨",
        "daogongxuid": "外发电镀",
        "daopeijianleixing": "金属色架",
        "wuliaodangan": "GME0679-55-35J",
        "baogong": 1,
        "liuchengtu": null,
        "gongjia": null,
        "beizhu": null,
        "create_user": {
            "id": 1,
            "name": "系统管理员",
            "avatar": "[]",
            "info": null
        },
        "update_user": null,
        "owner_frame": null,
        "owner_user": null,
        "is_share": false,
        "wuliaodanganid": 78821
    },
    {
        "id": 131028,
        "user_id": {
            "name": "系统管理员",
            "type": ""
        },
        "update_user_id": {
            "name": "",
            "type": ""
        },
        "frame_id": "",
        "owner_user_id": {
            "name": "",
            "type": ""
        },
        "created_at": null,
        "updated_at": null,
        "peijianxuhao": 1,
        "daogongxu": "包装装配",
        "shengchanzhouqi": 4,
        "gongyinandu": null,
        "laiyuan": "外发",
        "peijianleixing": "金属色架",
        "bengongxuid": "外发电镀",
        "bengongxu": "外发电镀",
        "daogongxuid": "包装装配",
        "daopeijianleixing": "",
        "wuliaodangan": "GME0679-55-35J",
        "baogong": 1,
        "liuchengtu": null,
        "gongjia": null,
        "beizhu": null,
        "create_user": {
            "id": 1,
            "name": "系统管理员",
            "avatar": "[]",
            "info": null
        },
        "update_user": null,
        "owner_frame": null,
        "owner_user": null,
        "is_share": false,
        "wuliaodanganid": 78821
    },
    {
        "id": 122214,
        "user_id": {
            "name": "系统管理员",
            "type": ""
        },
        "update_user_id": {
            "name": "",
            "type": ""
        },
        "frame_id": "",
        "owner_user_id": {
            "name": "",
            "type": ""
        },
        "created_at": null,
        "updated_at": null,
        "peijianxuhao": 2,
        "daogongxu": "成品仓",
        "shengchanzhouqi": 7,
        "gongyinandu": null,
        "laiyuan": "自制",
        "peijianleixing": "金属色架",
        "bengongxuid": "包装装配",
        "bengongxu": "包装装配",
        "daogongxuid": "成品仓",
        "daopeijianleixing": "",
        "wuliaodangan": "GME0679-55-35J",
        "baogong": 1,
        "liuchengtu": null,
        "gongjia": null,
        "beizhu": null,
        "create_user": {
            "id": 1,
            "name": "系统管理员",
            "avatar": "[]",
            "info": null
        },
        "update_user": null,
        "owner_frame": null,
        "owner_user": null,
        "is_share": false,
        "wuliaodanganid": 78821
    },
    {
        "id": 65808,
        "user_id": {
            "name": "系统管理员",
            "type": ""
        },
        "update_user_id": {
            "name": "",
            "type": ""
        },
        "frame_id": "",
        "owner_user_id": {
            "name": "",
            "type": ""
        },
        "created_at": null,
        "updated_at": null,
        "peijianxuhao": 1,
        "daogongxu": "打脾",
        "shengchanzhouqi": 4,
        "gongyinandu": null,
        "laiyuan": "自制",
        "peijianleixing": "胶脾",
        "bengongxuid": "仓库脾开料",
        "bengongxu": "仓库脾开料",
        "daogongxuid": "打脾",
        "daopeijianleixing": "",
        "wuliaodangan": "GME0679-55-35J",
        "baogong": 1,
        "liuchengtu": null,
        "gongjia": null,
        "beizhu": null,
        "create_user": {
            "id": 1,
            "name": "系统管理员",
            "avatar": "[]",
            "info": null
        },
        "update_user": null,
        "owner_frame": null,
        "owner_user": null,
        "is_share": false,
        "wuliaodanganid": 78821
    },
    {
        "id": 13044,
        "user_id": {
            "name": "系统管理员",
            "type": ""
        },
        "update_user_id": {
            "name": "",
            "type": ""
        },
        "frame_id": "",
        "owner_user_id": {
            "name": "",
            "type": ""
        },
        "created_at": null,
        "updated_at": null,
        "peijianxuhao": 2,
        "daogongxu": "滚桶",
        "shengchanzhouqi": 7,
        "gongyinandu": null,
        "laiyuan": "自制",
        "peijianleixing": "胶脾",
        "bengongxuid": "打脾",
        "bengongxu": "打脾",
        "daogongxuid": "滚桶",
        "daopeijianleixing": "",
        "wuliaodangan": "GME0679-55-35J",
        "baogong": 1,
        "liuchengtu": null,
        "gongjia": null,
        "beizhu": null,
        "create_user": {
            "id": 1,
            "name": "系统管理员",
            "avatar": "[]",
            "info": null
        },
        "update_user": null,
        "owner_frame": null,
        "owner_user": null,
        "is_share": false,
        "wuliaodanganid": 78821
    },
    {
        "id": 125777,
        "user_id": {
            "name": "系统管理员",
            "type": ""
        },
        "update_user_id": {
            "name": "",
            "type": ""
        },
        "frame_id": "",
        "owner_user_id": {
            "name": "",
            "type": ""
        },
        "created_at": null,
        "updated_at": null,
        "peijianxuhao": 3,
        "daogongxu": "CNC",
        "shengchanzhouqi": 5,
        "gongyinandu": null,
        "laiyuan": "自制",
        "peijianleixing": "胶圈",
        "bengongxuid": "滚桶",
        "bengongxu": "滚桶",
        "daogongxuid": "CNC",
        "daopeijianleixing": "",
        "wuliaodangan": "GME0679-55-35J",
        "baogong": 1,
        "liuchengtu": null,
        "gongjia": null,
        "beizhu": null,
        "create_user": {
            "id": 1,
            "name": "系统管理员",
            "avatar": "[]",
            "info": null
        },
        "update_user": null,
        "owner_frame": null,
        "owner_user": null,
        "is_share": false,
        "wuliaodanganid": 78821
    },
    {
        "id": 46269,
        "user_id": {
            "name": "系统管理员",
            "type": ""
        },
        "update_user_id": {
            "name": "",
            "type": ""
        },
        "frame_id": "",
        "owner_user_id": {
            "name": "",
            "type": ""
        },
        "created_at": null,
        "updated_at": null,
        "peijianxuhao": 4,
        "daogongxu": "精雕",
        "shengchanzhouqi": 10,
        "gongyinandu": null,
        "laiyuan": "自制",
        "peijianleixing": "胶圈",
        "bengongxuid": "CNC",
        "bengongxu": "CNC",
        "daogongxuid": "精雕",
        "daopeijianleixing": "",
        "wuliaodangan": "GME0679-55-35J",
        "baogong": 1,
        "liuchengtu": null,
        "gongjia": null,
        "beizhu": null,
        "create_user": {
            "id": 1,
            "name": "系统管理员",
            "avatar": "[]",
            "info": null
        },
        "update_user": null,
        "owner_frame": null,
        "owner_user": null,
        "is_share": false,
        "wuliaodanganid": 78821
    },
    {
        "id": 6226,
        "user_id": {
            "name": "系统管理员",
            "type": ""
        },
        "update_user_id": {
            "name": "",
            "type": ""
        },
        "frame_id": "",
        "owner_user_id": {
            "name": "",
            "type": ""
        },
        "created_at": null,
        "updated_at": null,
        "peijianxuhao": 5,
        "daogongxu": "胶打磨",
        "shengchanzhouqi": 1,
        "gongyinandu": null,
        "laiyuan": "自制",
        "peijianleixing": "胶圈",
        "bengongxuid": "精雕",
        "bengongxu": "精雕",
        "daogongxuid": "胶打磨",
        "daopeijianleixing": "",
        "wuliaodangan": "GME0679-55-35J",
        "baogong": 1,
        "liuchengtu": null,
        "gongjia": null,
        "beizhu": null,
        "create_user": {
            "id": 1,
            "name": "系统管理员",
            "avatar": "[]",
            "info": null
        },
        "update_user": null,
        "owner_frame": null,
        "owner_user": null,
        "is_share": false,
        "wuliaodanganid": 78821
    },
    {
        "id": 88410,
        "user_id": {
            "name": "系统管理员",
            "type": ""
        },
        "update_user_id": {
            "name": "",
            "type": ""
        },
        "frame_id": "",
        "owner_user_id": {
            "name": "",
            "type": ""
        },
        "created_at": null,
        "updated_at": null,
        "peijianxuhao": 6,
        "daogongxu": "包装装配",
        "shengchanzhouqi": 3,
        "gongyinandu": null,
        "laiyuan": "自制",
        "peijianleixing": "胶圈",
        "bengongxuid": "胶打磨",
        "bengongxu": "胶打磨",
        "daogongxuid": "包装装配",
        "daopeijianleixing": "金属色架",
        "wuliaodangan": "GME0679-55-35J",
        "baogong": 1,
        "liuchengtu": null,
        "gongjia": null,
        "beizhu": null,
        "create_user": {
            "id": 1,
            "name": "系统管理员",
            "avatar": "[]",
            "info": null
        },
        "update_user": null,
        "owner_frame": null,
        "owner_user": null,
        "is_share": false,
        "wuliaodanganid": 78821
    },
    {
        "id": 35602,
        "user_id": {
            "name": "系统管理员",
            "type": ""
        },
        "update_user_id": {
            "name": "",
            "type": ""
        },
        "frame_id": "",
        "owner_user_id": {
            "name": "",
            "type": ""
        },
        "created_at": null,
        "updated_at": null,
        "peijianxuhao": 1,
        "daogongxu": "CNC前工序",
        "shengchanzhouqi": 5,
        "gongyinandu": null,
        "laiyuan": "自制",
        "peijianleixing": "胶圈",
        "bengongxuid": "仓库圈开料",
        "bengongxu": "仓库圈开料",
        "daogongxuid": "CNC前工序",
        "daopeijianleixing": "",
        "wuliaodangan": "GME0679-55-35J",
        "baogong": 1,
        "liuchengtu": null,
        "gongjia": null,
        "beizhu": null,
        "create_user": {
            "id": 1,
            "name": "系统管理员",
            "avatar": "[]",
            "info": null
        },
        "update_user": null,
        "owner_frame": null,
        "owner_user": null,
        "is_share": false,
        "wuliaodanganid": 78821
    },
    {
        "id": 110007,
        "user_id": {
            "name": "系统管理员",
            "type": ""
        },
        "update_user_id": {
            "name": "",
            "type": ""
        },
        "frame_id": "",
        "owner_user_id": {
            "name": "",
            "type": ""
        },
        "created_at": null,
        "updated_at": null,
        "peijianxuhao": 2,
        "daogongxu": "滚桶",
        "shengchanzhouqi": 0,
        "gongyinandu": null,
        "laiyuan": "自制",
        "peijianleixing": "胶圈",
        "bengongxuid": "CNC前工序",
        "bengongxu": "CNC前工序",
        "daogongxuid": "滚桶",
        "daopeijianleixing": "",
        "wuliaodangan": "GME0679-55-35J",
        "baogong": 1,
        "liuchengtu": null,
        "gongjia": null,
        "beizhu": null,
        "create_user": {
            "id": 1,
            "name": "系统管理员",
            "avatar": "[]",
            "info": null
        },
        "update_user": null,
        "owner_frame": null,
        "owner_user": null,
        "is_share": false,
        "wuliaodanganid": 78821
    },
    {
        "id": 125777,
        "user_id": {
            "name": "系统管理员",
            "type": ""
        },
        "update_user_id": {
            "name": "",
            "type": ""
        },
        "frame_id": "",
        "owner_user_id": {
            "name": "",
            "type": ""
        },
        "created_at": null,
        "updated_at": null,
        "peijianxuhao": 3,
        "daogongxu": "CNC",
        "shengchanzhouqi": 5,
        "gongyinandu": null,
        "laiyuan": "自制",
        "peijianleixing": "胶圈",
        "bengongxuid": "滚桶",
        "bengongxu": "滚桶",
        "daogongxuid": "CNC",
        "daopeijianleixing": "",
        "wuliaodangan": "GME0679-55-35J",
        "baogong": 1,
        "liuchengtu": null,
        "gongjia": null,
        "beizhu": null,
        "create_user": {
            "id": 1,
            "name": "系统管理员",
            "avatar": "[]",
            "info": null
        },
        "update_user": null,
        "owner_frame": null,
        "owner_user": null,
        "is_share": false,
        "wuliaodanganid": 78821
    },
    {
        "id": 46269,
        "user_id": {
            "name": "系统管理员",
            "type": ""
        },
        "update_user_id": {
            "name": "",
            "type": ""
        },
        "frame_id": "",
        "owner_user_id": {
            "name": "",
            "type": ""
        },
        "created_at": null,
        "updated_at": null,
        "peijianxuhao": 4,
        "daogongxu": "精雕",
        "shengchanzhouqi": 10,
        "gongyinandu": null,
        "laiyuan": "自制",
        "peijianleixing": "胶圈",
        "bengongxuid": "CNC",
        "bengongxu": "CNC",
        "daogongxuid": "精雕",
        "daopeijianleixing": "",
        "wuliaodangan": "GME0679-55-35J",
        "baogong": 1,
        "liuchengtu": null,
        "gongjia": null,
        "beizhu": null,
        "create_user": {
            "id": 1,
            "name": "系统管理员",
            "avatar": "[]",
            "info": null
        },
        "update_user": null,
        "owner_frame": null,
        "owner_user": null,
        "is_share": false,
        "wuliaodanganid": 78821
    },
    {
        "id": 6226,
        "user_id": {
            "name": "系统管理员",
            "type": ""
        },
        "update_user_id": {
            "name": "",
            "type": ""
        },
        "frame_id": "",
        "owner_user_id": {
            "name": "",
            "type": ""
        },
        "created_at": null,
        "updated_at": null,
        "peijianxuhao": 5,
        "daogongxu": "胶打磨",
        "shengchanzhouqi": 1,
        "gongyinandu": null,
        "laiyuan": "自制",
        "peijianleixing": "胶圈",
        "bengongxuid": "精雕",
        "bengongxu": "精雕",
        "daogongxuid": "胶打磨",
        "daopeijianleixing": "",
        "wuliaodangan": "GME0679-55-35J",
        "baogong": 1,
        "liuchengtu": null,
        "gongjia": null,
        "beizhu": null,
        "create_user": {
            "id": 1,
            "name": "系统管理员",
            "avatar": "[]",
            "info": null
        },
        "update_user": null,
        "owner_frame": null,
        "owner_user": null,
        "is_share": false,
        "wuliaodanganid": 78821
    },
    {
        "id": 88410,
        "user_id": {
            "name": "系统管理员",
            "type": ""
        },
        "update_user_id": {
            "name": "",
            "type": ""
        },
        "frame_id": "",
        "owner_user_id": {
            "name": "",
            "type": ""
        },
        "created_at": null,
        "updated_at": null,
        "peijianxuhao": 6,
        "daogongxu": "包装装配",
        "shengchanzhouqi": 3,
        "gongyinandu": null,
        "laiyuan": "自制",
        "peijianleixing": "胶圈",
        "bengongxuid": "胶打磨",
        "bengongxu": "胶打磨",
        "daogongxuid": "包装装配",
        "daopeijianleixing": "金属色架",
        "wuliaodangan": "GME0679-55-35J",
        "baogong": 1,
        "liuchengtu": null,
        "gongjia": null,
        "beizhu": null,
        "create_user": {
            "id": 1,
            "name": "系统管理员",
            "avatar": "[]",
            "info": null
        },
        "update_user": null,
        "owner_frame": null,
        "owner_user": null,
        "is_share": false,
        "wuliaodanganid": 78821
    }
], // 这里应该填充您提供的原始数据
      treeData: []
    };
  },
  created() {
    this.treeData = this.buildReverseTree(this.rawData);
    console.log("this", this.treeData)
  },
  methods: {
    buildReverseTree(data) {
      // 1. 创建节点映射表和根节点集合
      const nodeMap = new Map(); // { daogongxuid: node }
      const rootNodes = [];       // 存储根节点（没有子节点的节点）

      // 2. 初始化所有节点并建立映射
      data.forEach(item => {
        const node = {
          id: item.id,
          name: item.daogongxu,
          peijianleixing: item.peijianleixing,
          bengongxuid: item.bengongxuid,
          daogongxuid: item.daogongxuid,
          shengchanzhouqi: item.shengchanzhouqi,
          children: []
        };
        nodeMap.set(node.daogongxuid, node);
      });

      // 3. 构建反向父子关系
      data.forEach(item => {
        const parentNode = nodeMap.get(item.daogongxuid);
        const childNode = nodeMap.get(item.bengongxuid);

        if (parentNode && childNode) {
          // 检查是否已存在相同 bengongxuid 的子节点
          const existingChildIndex = parentNode.children.findIndex(
            child => child.bengongxuid === childNode.bengongxuid
          );

          if (existingChildIndex !== -1) {
            // 如果已存在相同 bengongxuid 的子节点
            const existingChild = parentNode.children[existingChildIndex];
            
            if (existingChild.peijianleixing === childNode.peijianleixing) {
              // 类型相同，合并子级
              existingChild.children = [...existingChild.children, ...childNode.children];
            } else {
              // 类型不同，添加为同级节点
              parentNode.children.push(childNode);
            }
          } else {
            // 不存在则直接添加
            parentNode.children.push(childNode);
          }
        }
      });

      // 4. 识别根节点（没有作为 bengongxuid 出现的 daogongxuid）
      const allBengongxuids = new Set(data.map(item => item.bengongxuid));
      
      nodeMap.forEach(node => {
        if (!allBengongxuids.has(node.daogongxuid)) {
          rootNodes.push(node);
        }
      });
      console.log("node", node)
      // 5. 清理临时字段并格式化输出
      function formatNode(node) {
        const { bengongxuid, daogongxuid, ...rest } = node;
        return {
          ...rest,
          children: node.children.map(formatNode)
        };
      }

      return rootNodes.map(formatNode);
    }
  }
};
</script>

<style>
.tree-node {
  text-align: left;
  margin-left: 20px;
}
</style>